package com.micangcaihang.common.base.fragment;

import android.databinding.ViewDataBinding;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.micangcaihang.common.R;
import com.micangcaihang.common.base.mvp.BasePresenter;
import com.micangcaihang.common.base.mvp.IBaseListView;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayonggmail@gmail.com
 *     time   : 2017/06/28
 *     desc   : 普通list的baseFragment
 *     version: 1.0
 * </pre>
 */

public abstract class BaseListFragment<P extends BasePresenter, A extends BaseQuickAdapter, B extends ViewDataBinding>
        extends BaseFragment<P, B> implements BaseQuickAdapter.RequestLoadMoreListener, IBaseListView {
    private final String TAG = getClass().getSimpleName();
    /**
     * 普通list布局
     */
    protected static final int LINEAR_LAYOUT_MANAGER = 0;
    /**
     * grid布局
     */
    protected static final int GRID_LAYOUT_MANAGER = 1;
    /**
     * 瀑布流布局
     */
    protected static final int STAGGERED_GRID_LAYOUT_MANAGER = 2;
    /**
     * 默认为0单行布局
     */
    private int mListType = 0;
    /**
     * 排列方式默认垂直
     */
    private boolean mIsVertical = true;
    /**
     * grid布局与瀑布流布局默认行数
     */
    private int mSpanCount = 1;


    protected RecyclerView mRecyclerView;

    protected A mAdapter;
    /**
     * 子布局id
     */
    private int layoutResId = -1;

    private LoadMoreView loadMoreView;
    /**
     * 每页数量  默认10
     */
    private int pageSize = 10;


    public int getPageSize() {
        return pageSize;
    }
    /**
     * 初始化view
     *
     * @param rootView 资源布局view
     */
    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    }
    /**
     * 初始化对象
     */
    @Override
    protected void initData() {
        super.initData();
        initRecyclerViewSetting();
        chooseListType(mListType, mIsVertical);
        initAdapter();
        if (mAdapter == null) {
            throw new RuntimeException("adapter is null!");
        }
        mAdapter.setLoadMoreView(getLoadMoreView());
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * recycleView的设置
     */
    protected abstract void initAdapter();

    /**
     * 初始化各种状态处理
     * 在这个方法里处理的是recyclerview的所有的初始化，
     * 包括对他的展示形式，是list或grid或瀑布流
     */
    protected abstract void initRecyclerViewSetting();

    /**
     * 设置load界面的多种状态 没有更多、loading、加载失败三种三种状态
     *
     * @param loadMoreView load界面多状态布局
     */
    protected void setLoadMordTypeLayout(LoadMoreView loadMoreView) {
        this.loadMoreView = loadMoreView;
    }

    public LoadMoreView getLoadMoreView() {
        return loadMoreView == null ? new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.quick_view_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return R.id.load_more_loading_view;
            }

            @Override
            protected int getLoadFailViewId() {
                return R.id.load_more_load_fail_view;
            }

            @Override
            protected int getLoadEndViewId() {
                return R.id.load_more_load_end_view;
            }
        } : loadMoreView;
    }

    /**
     * 是否打开加载更多
     */
    protected void openLoadMore() {
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
    }

    /**
     * @param type       布局管理type
     * @param isVertical 是否是垂直的布局 ，true垂直布局，false横向布局
     */
    protected void setListType(int type, boolean isVertical) {
        mListType = type;
        mIsVertical = isVertical;
    }

    /**
     * 为grid样式和瀑布流设置横向或纵向数量
     *
     * @param spanCount 数量
     */
    protected void setSpanCount(int spanCount) {
        if (spanCount > 0)
            mSpanCount = spanCount;
    }

    /**
     * @param listType 选择布局种类
     */
    protected void chooseListType(int listType, boolean isVertical) {
        switch (listType) {
            case LINEAR_LAYOUT_MANAGER:
                //设置布局管理器
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());

                linearLayoutManager.setOrientation(isVertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);

                mRecyclerView.setLayoutManager(linearLayoutManager);
                break;
            case GRID_LAYOUT_MANAGER:

                GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), mSpanCount);

                gridLayoutManager.setOrientation(isVertical ? GridLayoutManager.VERTICAL : GridLayoutManager.HORIZONTAL);

                mRecyclerView.setLayoutManager(gridLayoutManager);
                break;
            case STAGGERED_GRID_LAYOUT_MANAGER:
                //设置布局管理器
                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager
                        (mSpanCount, isVertical ? StaggeredGridLayoutManager.VERTICAL : StaggeredGridLayoutManager.HORIZONTAL);

                mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
                break;
            default:
                //设置布局管理器
                LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());

                layoutManager.setOrientation(isVertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);

                mRecyclerView.setLayoutManager(layoutManager);
                break;
        }
    }

    /*BaseQuickAdapter.RequestLoadMoreListener start*/
    @Override
    public void onLoadMoreRequested() {
        mSwipeRefresh.setEnabled(false);
        mPresenter.loadMoreData();
    }
    /*BaseQuickAdapter.RequestLoadMoreListener end*/

    /*IBaseList interface start*/
    @Override
    public void loadMoreFail(Throwable e) {
        mAdapter.loadMoreFail();
    }
    /*IBaseList interface end*/

    /**
     * 重写父类IBaseView的接口刷新加载操作
     * 刷新是显示刷新状态
     * 并且置为不可操作状态(只可刷新一次,刷新完才可以再次刷新)
     * 并且刷新时禁止加载更多
     */
    @Override
    public void showRefreshLoading() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefresh.setRefreshing(true);

                }
            });
        }
        mAdapter.setEnableLoadMore(false);
    }

    /**
     * 重写父类IBaseView的接口刷新完成操作
     * 刷新是隐藏刷新状态
     * 并且刷新完成时开启加载更多
     */
    @Override
    public void finishRefresh() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefresh.setRefreshing(false);
                    mSwipeRefresh.setEnabled(true);
                }
            },500);
        }
        mAdapter.setEnableLoadMore(true);
    }

}
